package com.tuanzi.loan.web.bean.product.model;

import com.tuanzi.loan.business.service.family.ActivityModelService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.view.family.ActivityModelView;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: hequnayu
 * @Date: 2019/7/8 17:38
 * @Description:
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanActivityModelInfoBean extends JSFInfoBean<ActivityModelView> {

    @Autowired
    private ActivityModelService modelService;

    private List<ActivityModelView.ContentItem> modelContentList;
    private ActivityModelView.ContentItem selectContentItem;
    private Integer contentFlag = 0;  //增加：0   修改：1
    private Integer rowIndex;

    @PostConstruct
    private void init() {
        String id = getParamFromRequest("id");
        if (StringUtils.isNotBlank(id)) {
            Long modelInfoId = NumberUtils.toLong(id);
            $ = modelService.findViewById(modelInfoId);
            modelContentList = $.getItems();
        } else {
            $ = new ActivityModelView();
            modelContentList = new ArrayList<>();
            changeStatusToAdd();
        }
        selectContentItem = new ActivityModelView.ContentItem();
    }

    /**
     * 保存模板配置
     */
    public void onSave() {
        if (!validateModel()){
            return;
        }
        $.setItems(modelContentList);
        modelService.saveActivityModelView($);
        changeStatusToView();
        addSuccessfullyMessage();
    }

    /**
     * 新增模板属性
     */
    public void onAddContent(){
        contentFlag = 0;
        selectContentItem = new ActivityModelView.ContentItem();
        update("modelInfoForm:model-content-add-dialog");
    }

    /**
     * 修改模板属性
     * @param rowIndex
     */
    public void onSeeContent(Integer rowIndex){
        contentFlag = 1;
        this.rowIndex = rowIndex;
        selectContentItem = modelContentList.get(rowIndex);
        update("modelInfoForm:model-content-add-dialog");
    }

    /**
     * 删除模板属性
     * @param rowIndex
     */
    public void onDeleteContent(Integer rowIndex){
        modelContentList.remove(rowIndex.intValue());
    }

    /**
     * 保存模板属性
     */
    public void onSaveContent(){
        if (!validateContent()){
            return ;
        }
        if (contentFlag == 0){  //新增属性
            modelContentList.add(selectContentItem);
        }else{
            modelContentList.set(this.rowIndex, selectContentItem);
        }
        update("modelInfoForm");
    }

    public boolean validateModel(){
        if (StringUtils.isEmpty($.getModelName())) {
            addErrorMessage("请输入模板名称");
            return false;
        }if ($.getModelType() == null) {
            addErrorMessage("请选择模板类型");
            return false;
        }
        return true;
    }

    public boolean validateContent(){
        if (StringUtils.isEmpty(selectContentItem.getName())) {
            addErrorMessage("请输入title");
            return false;
        }
        if (StringUtils.isEmpty(selectContentItem.getContent())) {
            addErrorMessage("请输入key名称");
            return false;
        }
        if (selectContentItem.getType() == null) {
            addErrorMessage("请输入类型");
            return false;
        }
        return true;
    }
}
